package com.lk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lk.bean.Clazz;
import com.lk.service.ClazzService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clazz")
public class ClazzController {

    @Resource
    private ClazzService clazzService;


    /**
     * 跳转班级列表页面视图
     *
     * @return
     */
    @GetMapping("/goClazzListView")
    public String goClazzListView() {
        return "/clazz/clazzList";
    }

    /**
     * 根据ids删除班级
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteClazz")
    @ResponseBody
    public Map<String,Object> deleteClazz(@RequestParam(value = "ids[]",required = true) Integer[] ids){
        Map<String,Object> result = new HashMap<>();
        Integer row = clazzService.deleteClazz(ids);
        if (row>0){
            result.put("success",true);
        }else {
            result.put("success",false);
            result.put("msg","删除失败");
        }
        return result;
    }


    /**
     * 添加班级信息
     *
     * @param clazz
     * @return
     */
    @PostMapping("/addClazz")
    @ResponseBody
    public Map<String,Object> addClazz(Clazz clazz){
        Map<String,Object> result = new HashMap<>();
        //判断请求参数是否为空
        if (clazz == null){
            result.put("success",false);
            result.put("msg","请求参数为空");
            return result;
        }
        //根据名称判断要添加的班级名是否存在
        String name = clazz.getName();
        Clazz clazz1 = clazzService.findByName(name);
        if (clazz1 != null){
            result.put("success",false);
            result.put("msg","班级名已经存在");
        }
        int row = clazzService.addClazz(clazz);
        if (row ==1){
            result.put("success",true);
        }else {
            result.put("success",false);
            result.put("msg","添加失败");
        }
        return result;
    }


    /**
     * 修改班级信息
     *
     * @param clazz
     * @return
     */
    @PostMapping("/editClazz")
    @ResponseBody
    public Map<String,Object> editClazz(Clazz clazz){
        Map<String,Object> result = new HashMap<>();
        //根据name查询是否已经存在同名的班级
        Clazz clazz1 = clazzService.findByName(clazz.getName());
        //自己
        if (clazz1 != null && clazz1.getId() == clazz.getId()) {
            int row = clazzService.updateClazz(clazz);
            if (row == 1) {
                result.put("success", true);
                return result;
            } else {
                result.put("success", false);
                result.put("mag", "修改失败");
                return result;
            }
        } else if (clazz1 != null && clazz1.getId() != clazz.getId()) { //名字存在但不是自己
            result.put("success", false);
            result.put("mag", "用户名已存在");
            return result;
        } else { //班级名不存在
            int row = clazzService.updateClazz(clazz);
            if (row == 1) {
                result.put("success", true);
                return result;
            } else {
                result.put("success", false);
                result.put("mag", "修改失败");
                return result;
            }
        }
    }


    /**
     * 分页获取clazz数据
     *
     * @param page
     * @param rows
     * @param clazzname
     * @return
     */
    @PostMapping("/getClazzList")
    @ResponseBody
    public Map<String, Object> getClazzList(Integer page, Integer rows, String clazzname) {
        Map<String, Object> result = new HashMap<>();
        Clazz clazz = new Clazz();
        clazz.setName(clazzname);
        //设置每页记录数
        PageHelper.startPage(page, rows);
        //获取指定的班级信息，若没有则获取全部数据
        List<Clazz> list = clazzService.getClazzList(clazz);
        PageInfo<Clazz> pageInfo = new PageInfo<>(list);

        //总页数
        long total = pageInfo.getTotal();

        result.put("total", total);
        result.put("rows", list);
        return result;
    }

}
